package com.ggh.jinjilive.views.live.music.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemMusicListBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.live.MusicList;

import java.util.List;

public class BGMRecyclerViewAdapter extends RecyclerView.Adapter<BGMRecyclerViewAdapter.ItemViewHolder> {

    private List<MusicList> musicListList;

    public void setAdd(List<MusicList> musicListList) {
        this.musicListList = musicListList;
        notifyDataSetChanged();
    }

    public void add(List<MusicList> musicListList) {
        if(null != this.musicListList){
            this.musicListList.addAll(musicListList);
        }else {
            this.musicListList = musicListList;
        }
        notifyDataSetChanged();
    }



    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMusicListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_music_list, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        MusicList musicList = musicListList.get(position);
        if(null != musicList){
            holder.binding.tvBgmTitle.setText(musicList.getTitle());
            holder.binding.tvBgmActor.setText(musicList.getAuthor());
            holder.binding.llMusic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,musicList);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == musicListList ? 0 : musicListList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemMusicListBinding binding;

        public ItemViewHolder(@NonNull ItemMusicListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}